package cn.xtits.xtf.common.spring.db.readwrite;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;


public class DynamicDataSourceTransactionManager extends DataSourceTransactionManager {

    @Override
    protected void doBegin(Object transaction, TransactionDefinition definition) {
        boolean readOnly = definition.isReadOnly();
        if (readOnly)
            DynamicReadWriterDataSourceHolder.setSlave();
        else
            DynamicReadWriterDataSourceHolder.setMaster();
        super.doBegin(transaction, definition);
    }

    @Override
    protected void doCleanupAfterCompletion(Object transaction) {
        super.doCleanupAfterCompletion(transaction);
        DynamicReadWriterDataSourceHolder.clear();
    }
}
